#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""简单命令行计算器 - 支持加减乘除四则运算"""


def add(a, b):
    """加法运算"""
    return a + b


def subtract(a, b):
    """减法运算"""
    return a - b


def multiply(a, b):
    """乘法运算"""
    return a * b


def divide(a, b):
    """除法运算（包含除零错误处理）"""
    if b == 0:
        raise ValueError("错误：除数不能为零")
    return a / b


def calculate():
    """计算器交互主函数"""
    print("===== 简单命令行计算器 =====")
    
    while True:
        try:
            # 获取用户输入
            num1 = float(input("请输入第一个数字: "))
            operator = input("请输入运算符(+, -, *, /): ")
            num2 = float(input("请输入第二个数字: "))
            
            # 根据运算符调用对应函数
            if operator == '+':
                result = add(num1, num2)
            elif operator == '-':
                result = subtract(num1, num2)
            elif operator == '*':
                result = multiply(num1, num2)
            elif operator == '/':
                result = divide(num1, num2)
            else:
                print("错误：不支持的运算符，请重试！")
                continue
            
            # 输出结果
            print(f"计算结果: {num1} {operator} {num2} = {result}")
            
            # 询问是否继续计算
            continue_calc = input("是否继续计算？(y/n): ").lower()
            if continue_calc != 'y':
                break
                
        except ValueError as e:
            print(f"输入错误: {e}，请重新输入")
        except Exception as e:
            print(f"发生错误: {e}")


if __name__ == "__main__":
    calculate()